package seGobangClient.gaming;

import AIModel.Spot;
import AIModel.Strategy;
import java.util.*;
import javax.vecmath.Point2i;
import seGobangClient.CoordinateTransfer.CoTransfer;
import seGobangClient.ISceneController;
import seGobangClient.Rule;

/**
 *
 * @author plus
 */
public class AIPlayer implements IPlayer{
    private static ISceneController MyController;
    private String Name;
    private int iTurns, iRegrets;
    private int iTime;                                                          //剩余时间，单位s
    private byte chess_flag;
    private Timer timerUpdateInfo;
    private Rule MyRule;
    private Strategy AI=new Strategy(15);

    public AIPlayer(String Name,byte chessflag,Rule rule) {
        this.chess_flag=chessflag;
        this.Name = Name;
        MyRule=rule;
        resetPlayer();
    }
    
    public void resetPlayer(){
        iTurns=0;
        iRegrets=MyRule.RegretTimes;
        iTime=MyRule.TimeLimit;
        if (timerUpdateInfo!=null) timerUpdateInfo.cancel();
    }
    
    public void setController(ISceneController Controller){
        MyController=Controller;
    }
    
    public int getLastRegrets() {
        return iRegrets;
    }

    public int getLastTime() {
        return iTime;
    }

    public String getPlayerName() {
        return Name;
    }

    public int getTurns() {
        return iTurns;
    }

    public void turnToMe() {   
        timerUpdateInfo=new Timer();
        timerUpdateInfo.schedule(new TimerTask(){
            public void run(){
                MyController.doEvent("UpdatePlayerInfo", this);
                if (iTime==0)
                    MyController.doEvent("SomeoneTimeout", this);
                else 
                    if (iTime>0) iTime--;
            }
        }, 1000, 1000
        );
        MyController.doEvent("MarkCurrentPlayer", this);
        MyController.doEvent("DisableLocalInput",null);
        
        aiDescision();
    }

    public void turnOver(boolean isRegret) {
        if (timerUpdateInfo != null) timerUpdateInfo.cancel();
       // timerUpdateInfo=null;
        
        if(isRegret)
           this.iTurns--; 
        else
            this.iTurns++;
        
    }

    public void putChess(Point2i pos) {
        if (this.chess_flag==-1)
            MyController.doEvent("BlackChessPut", pos);
        else
            MyController.doEvent("WhiteChessPut", pos);
    }

    public void gameResult(int iResult) {
       if (timerUpdateInfo != null) timerUpdateInfo.cancel();
        System.err.println(this.getPlayerName()+" "+iResult);
        if (iResult==this.RESULT_WIN || iResult==this.RESULT_OPP_SURRENDERED || iResult==this.RESULT_OPP_FORBIDDEN)
            MyController.doEvent("AIFxWin", new Point2i());
    }
    
    
    private void aiDescision(){
        CoTransfer trans=CoTransfer.getInstance();
        int[][] ChessBox=trans.MatToMatrix(((GamingController)MyController).currentChessBoard.Chesses);
        Spot res=new Spot();
        res=AI.makeStrategy(ChessBox, this.chess_flag, 5);
        Point2i target=new Point2i(res.getX()-7,res.getY()-7);
            MyController.doEvent("AIFx", target);
           
        
    }
    
    public void regret() {
    }

    public void surrender() {
    }
    
    public byte getChessFlag(){
        return this.chess_flag;
    }
    
}
